//
//  StockMonitor.m
//  Stock
//
//  Created by blacksun on 11-7-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StockRadar.h"


@implementation StockRadar
@synthesize name;
@synthesize type;
@synthesize status;
@synthesize stockId;
@synthesize targetValue;
@synthesize currentValue;
@synthesize targetPercent;
@synthesize repeat;
@synthesize lastDate;

-(id)init{
    self = [super init];
    if (self) {
        statusNames = [[NSArray alloc] initWithObjects:@"未启用",@"已失效",@"预警中",nil];
        return self;
    }
    return nil;
}
-(void)dealloc{
    [name release];
    [stockId release];
    [targetValue release];
    [currentValue release];
    [targetPercent release];
    [lastDate release];
    [statusNames release];
    [super dealloc];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    NSLog(@"key:%@ not found",key);
}
+(StockRadar *)findByStockId:(NSString *)aStockId andType:(int)aType{
    NSString *criteria = [NSString stringWithFormat:@"where stock_id='%@' and type='%d'",aStockId,aType];
    StockRadar *monitor = (StockRadar *)[StockRadar findFirstByCriteria:criteria];
    if (!monitor) {
        monitor = [[[StockRadar alloc] init] autorelease];
        if (aType == kStockMonitorTypeUp) {
            monitor.type = kStockMonitorTypeUp;
            monitor.name = kStockMonitorNameUp;
        }else{
            monitor.type = kStockMonitorTypeDown;
            monitor.name = kStockMonitorNameDown;
        }
        monitor.stockId = aStockId;
        monitor.status = 0;
        monitor.targetValue = @"--";
        monitor.targetPercent = @"--";
        monitor.repeat = NO;
    }
    return monitor;
}

-(NSString *)statusName{
    return [statusNames objectAtIndex:status];
}

@end
